package models

import (
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// PublishScheduleDevice 计划设备表
type PublishScheduleDevice struct {
	ID            string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	MerchantID    string `json:"merchant_id" gorm:"column:merchant_id;primaryKey;type:varchar(30);not null;default:'';index:merchant_id;comment:商户id"`
	ScheduleID    string `json:"schedule_id" gorm:"column:schedule_id;type:int;size:64;comment:计划ID"`
	RobotName     string `json:"robot_name" gorm:"index:idx_robotName_deletedAt,priority:1;column:robot_name;type:string;size:50;not null;default:'';comment:设备名称"`
	StartTime     int64  `json:"start_time" gorm:"column:start_time;type:int;size:64;not null;default:0;comment:开始时间"`
	EndTime       int64  `json:"end_time" gorm:"column:end_time;type:int;size:64;not null;default:0;comment:结束时间"`
	PublishTime   int64  `json:"publish_time" gorm:"column:publish_time;type:int;size:64;not null;default:0;comment:投放时间"`
	PublishStatus int    `json:"publish_status" gorm:"column:publish_status;type:tinyint(1);not null;default:0;comment:投放状态:0未投放 1投放中 2投放失败 3投放成功"`
	PublishType   int    `json:"publish_type" gorm:"column:publish_type;type:tinyint(1);not null;default:0;comment:投放类型【1新增 2编辑 3删除】"`
	TerminalType  int    `json:"terminal_type" gorm:"column:terminal_type;type:tinyint(1);not null;default:1;comment:终端类型【1-97 2-98】"`
	CreatedAt     int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;not null;default:0;index:created_at;comment:创建时间"`
	UpdatedAt     int64  `json:"updated_at" gorm:"column:updated_at;autoUpdateTime;type:int;size:64;not null;default:0;index:updated_at;comment:更新时间"`
	DeletedAt     int64  `json:"deleted_at" gorm:"index:idx_robotName_deletedAt,priority:2;column:deleted_at;type:int;size:64;not null;default:0;comment:删除时间"`
}

// TableName 表名
func (PublishScheduleDevice) TableName() string {
	return "publish_schedule_device"
}

// BeforeCreate 自增id
func (l *PublishScheduleDevice) BeforeCreate(tx *gorm.DB) (err error) {
	l.CreatedAt = time.Now().Unix()
	if l.ID == "" {
		// id为空时，自动添加id
		uuid, err := helper.UUID{}.String()
		if err != nil {
			return err
		}
		l.ID = uuid
	}
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &PublishScheduleDevice{})
}
